{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Cheat Sheet\n",
    "\n",
    "Basic cheatsheet for Python mostly based on the book written by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.\n",
    "\n",
    "## Read It\n",
    "\n",
    "- [Website](https://www.pythoncheatsheet.org)\n",
    "- [Github](https://github.com/wilfredinni/python-cheatsheet)\n",
    "- [PDF](https://github.com/wilfredinni/Python-cheatsheet/raw/master/python_cheat_sheet.pdf)\n",
    "- [Jupyter Notebook](https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks)\n",
    "\n",
    "## Exception Handling\n",
    "\n",
    "### Basic exception handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spam(divideBy):\n",
    "    try:\n",
    "        return 42 / divideBy\n",
    "    except ZeroDivisionError as e:\n",
    "        print('Error: Invalid argument: {}'.format(e))\n",
    "\n",
    "print(spam(2))\n",
    "print(spam(12))\n",
    "print(spam(0))\n",
    "print(spam(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Final code in exception handling\n",
    "\n",
    "Code inside the `finally` section is always executed, no matter if an exception has been raised or\n",
    "not, and even if an exception is not caught."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spam(divideBy):\n",
    "    try:\n",
    "        return 42 / divideBy\n",
    "    except ZeroDivisionError as e:\n",
    "        print('Error: Invalid argument: {}'.format(e))\n",
    "    finally:\n",
    "        print(\"-- division finished --\")\n",
    "\n",
    "print(spam(12))\n",
    "print(spam(0))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
